//
//  WmcAltitudeLabel.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/7/21.
//

import CoreLocation
import UIKit

public extension WmcAltitudeLabel {
    enum Format: Int {
        /// 米单位
        case meters
    }
}

// MARK: - [WmcAltitudeLabel]

open class WmcAltitudeLabel: WmcBaseSystemFieldLabel {
    // Open

    /// 显示格式
    open var format: Format = .meters {
        didSet { showText() }
    }

    /// 配置
    override open func config() {
        text = "海拔获取中..."

        // 监听定位
        WmcLocationManager.shared.locationStatus.subscribe(onNext: { [weak self] model in
            guard let weakSelf = self,
                  let model = model,
                  !weakSelf.styleMode.isStatic else { return }
            weakSelf.updateLocation(model.data?.location)
        }).disposed(by: rx.disposeBag)
    }

    // Internal

    /// 当前定位
    var currentLocation: CLLocation?

    /// 子类获取海拔的方法
    func updateLocation(_ location: CLLocation?) {
        YPGCD.Main { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.currentLocation = location
            weakSelf.showText()
        }
    }

    /// 显示文字
    func showText() {
        let status = WmcAuthorizedManager.location()
        if status.info.isAvailable {
            guard let currentLocation = currentLocation else { return }
            var str = ""
            // 直接转format
            switch format {
            case .meters:
                str = String(format: "%.2f米", currentLocation.altitude)
            }
            text = str
        } else {
            text = "未开启定位权限"
        }
    }
}
